﻿using System;
using System.Runtime.Serialization.Formatters.Binary;
using System.IO;

namespace Gems
{
    [Serializable]
    public class GossipMessage
    {
        public SuspectMatrix Matrix { get; set; }
        public string HostName { get; set; }

        public GossipMessage(SuspectMatrix matrix, string hostName)
        {
            Matrix = matrix;
            HostName = hostName;
        }

        public static GossipMessage FromBytes(byte[] buffer)
        {
            
            var serializer = new BinaryFormatter();
            var s = new MemoryStream(buffer);
            return serializer.Deserialize(s) as GossipMessage;
        }

        public byte[] ToBytes()
        {
            using (var stream = new MemoryStream())
            {
                var serializer = new BinaryFormatter();
                serializer.Serialize(stream, this);
                return stream.ToArray();
            }
        }
    }
}
